package org.yoki.edu.bg.radius.filter.app.listener;

import org.yoki.edu.bg.radius.filter.app.operator.AcctPacketLogoutOperator;
import org.yoki.edu.bg.radius.filter.app.operator.CheckPacketOperator;
import org.yoki.edu.bg.radius.filter.app.receiver.AcctRadiusPacketReceiver;
import org.yoki.edu.bg.radius.filter.app.receiver.CheckRadiusPacketReceiver;
import org.yoki.edu.bg.radius.filter.app.transit.SynCheckPacketTransit;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 * @author Sky$
 * @Description: TODO
 * @date 2018/1/4$ 16:26$
 */
@WebListener
public class ReceiverStartListener implements ServletContextListener {

    @Autowired
    private CheckRadiusPacketReceiver checkPacketReceiver;
    @Autowired
    private AcctRadiusPacketReceiver acctPacketReceiver;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        Thread threadCheck = new Thread(checkPacketReceiver);
        Thread threadAcct = new Thread(acctPacketReceiver);
        SynCheckPacketTransit transit = new SynCheckPacketTransit();
        //添加处理器
        transit.addObserver(new CheckPacketOperator());
        //添加转发器
        checkPacketReceiver.addObserver(transit);
        SynCheckPacketTransit acctTransit = new SynCheckPacketTransit();
        //添加处理器
        acctTransit.addObserver(new AcctPacketLogoutOperator());
        //添加转发器
        acctPacketReceiver.addObserver(acctTransit);
        threadCheck.start();
        threadAcct.start();
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        checkPacketReceiver.setStopFlag(true);
        acctPacketReceiver.setStopFlag(true);
    }
}
